require 'albacore'
require_relative './tools/ruby/subversion'
require_relative './tools/ruby/versioning'

$project_name = 'SvnExternalUpdater'
$solution_file = 'SvnExternalUpdater.sln'
$assembly_info_file = 'src/CommonAssemblyInfo.cs'
$staging_area_dir = 'staging_area'

$configuration = ENV['config'] || 'Release'
fail "#{$configuration} is not a valid configuration" unless ['Debug', 'Release'].include? $configuration
$platform = ENV['platform'] || 'x86'
case $platform
when 'Any CPU', 'Any_CPU', 'AnyCPU'
    $platform = 'Any CPU'
    $build_output_dir = File.join(Dir.pwd, 'bin', $configuration, '/')
when 'x86', 'x64'
    $build_output_dir = File.join(Dir.pwd, 'bin', $platform, $configuration, '/')
else
    fail "#{$platform} is not a valid platform"
end

svn = Subversion.new(nil, nil)
build_number = svn.get_current_revision() || '0'
$version = Versioning::retrieve_dotnet_version($assembly_info_file, build_number)

puts
puts "Building #{$solution_file}..."
puts "Configuration: #{$configuration}"
puts "Platform: #{$platform}"
puts "Version: #{$version}"
puts

################################################################################

# Albacore configuration task run before any other task: configure default Albacore task properties
Albacore.configure do |config|
    config.log_level = :verbose
    config.nunit.command = $platform == 'x86' ? 'tools/nunit/nunit-console-x86.exe' : 'tools/nunit/nunit-console.exe'
    config.msbuild.solution = $solution_file
end

################################################################################

desc 'Default task: do nothing but initialization stuffs'
task :default

task :all => [:versioning, :rebuild, :artifacts]

################################################################################

desc 'Run a build of the whole solution'
msbuild :build do |t|
    t.properties :configuration => $configuration, :Platform => $platform, :OutDir => $build_output_dir
    t.targets :Build
end

desc 'Clean the whole solution'
msbuild :clean do |t|
    t.properties :configuration => $configuration, :Platform => $platform, :OutDir => $build_output_dir
    t.targets :Clean
end

desc 'Run a clean build of the whole solution'
task :rebuild => [:clean, :build]

desc 'Update assembly versions with current build number'
assemblyinfo :versioning do |t|
    t.input_file = $assembly_info_file
    t.version = $version
    t.file_version = $version
    t.output_file = $assembly_info_file
end

################################################################################

desc 'Run unit tests when nunit console'
nunit :unit_tests do |t|
    t.assemblies = FileList[File.join($build_output_dir, '*.Tests.dll')]
end

################################################################################

desc 'Create artifacts for current build configuration'
task :artifacts => [:prepare_staging_area,
                    :zip_artifacts,
                    :delete_staging_area]

# create and fill staging area with artifact items
task :prepare_staging_area do
    FileUtils.remove_dir($staging_area_dir) if File.directory? $staging_area_dir
    FileUtils.mkdir $staging_area_dir
    
    FileList[File.join($build_output_dir, '*.exe'),
             File.join($build_output_dir, '*.dll'),
             File.join($build_output_dir, '*.config'),
             File.join($build_output_dir, '*.netmodule')
             ].exclude(/.*Tests\.dll/,
                      /nunit\.framework\.dll/,
                      /Rhino\.Mocks\.dll/,
                      /Moq\.dll/,
                      /vshost\.exe/,
                      /SharpPlink-Win32\.svnExe/
             ).each do |file|
        FileUtils.cp file, $staging_area_dir, :verbose => true
    end
end

task :delete_staging_area do
    FileUtils.remove_dir($staging_area_dir) if File.directory? $staging_area_dir
end

zip :zip_artifacts do |zip|
    zip.directories_to_zip $staging_area_dir
    zip.output_file = $project_name
    zip.output_file += '-' + $platform.gsub(/ /, '') unless $platform == 'Any CPU'
    zip.output_file += '-' + $version + '.zip'
    zip.output_path = '.'
    puts "generating #{zip.output_file}..."
end
